<?php
include_once('includes.php'); 	

if($_SERVER["REQUEST_METHOD"]=="POST")
{
	if( !isSopcastLink($_POST["link"]) )
		exit('Invalid sopcast channel. <div class="button"> <input type="button" value="Close" onclick="$.fancybox.close()"/>  </div>');
	
	$sql = "select * from channel where match_id=".addslashes($_POST["id"])." and link='".$_POST["link"]."'";
	$channel=db_fetch_one($sql);
	
	if(!$channel)
	{
		$data_channel["match_id"] = $_POST["id"];
		$data_channel["name"] = $_POST["name"];
		$data_channel["link"] = $_POST["link"];
		$data_channel["type"] = "sopcast";
		$data_channel["vote"] = 0;
		$data_channel["is_suggest"] = 1;
		$channel_id=db_insert("channel",$data_channel);	
		
		exit(' Thank you for your suggestion link. Please refresh window to watch the match with your suggested link. <div class="button"> <input type="button" value="Close" onclick="$.fancybox.close()"/>  </div>');		
	}
	else
		exit('This channel already existed. <div class="button"> <input type="button" value="Close" onclick="$.fancybox.close()"/>  </div>');
		
}
else if(isset($_GET["id"]) && $_GET["id"]>0 )
{
	$id = (int) $_GET["id"];
	$sql = "select * from matchs where id=".addslashes($id);
	$match=db_fetch_one($sql);
		
}
else{
	exit("Can't find this match");
}

?>
<script type="text/javascript" src="/js/jquery.validate.js"></script>

<script>
if (typeof jQuery  == "undefined") {
	window.location="/";
}
$().ready(function() {
	var form = $("#submitform");
	jQuery.validator.messages.required = "";
	form.validate({
		submitHandler: function(frm) 
		{
			
			var postData = {};
			form.find(':input').each(function(){
				postData[$(this).attr('name')] = $(this).val();
			});

			
			$.ajax({
				type: "POST",
				url: "/popup-addchannel.php",
				data: postData,
				beforeSend: function(){
					$.fancybox.showActivity();
				}, 
				success: function(data)
				{
			 		$.fancybox.hideActivity();
					$(".popup .content").html(data);
					
				}
			 });
			 
			
		}
	});

});
</script>


<div class="popup">
	<span class="title">Gửi kênh sopcast cho trận "<?php echo $match->versus?>"</span>
	<div class="content">
			
		<form name="submitform" id="submitform" method="post" action="">
			<input type="hidden" name="id" value="<?php echo $match->id;?>"/>
			<p>
				<label>Kênh cung cấp</label>
				<input type="text" name="name" style="width:350px" class="required"/>
				
			</p>
			<p>
				<label>Link Sopcast</label>
				<input type="text" name="link" style="width:350px" class="required"/>
				
			</p>
			<div class="button"> <input type="submit" value="Gửi"/> <input type="button" value="Đóng" onclick="$.fancybox.close()"/>  </div>
		</form>	
	</div>
</div>